/*
 * Album.java
 *
 * Created on November 18, 2007, 8:29 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.piu.lib;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Person;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.media.MediaFileSource;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 *
 * @author spiros
 */
public class Album {
    private String name="";
    private Credentials credentials;
    public static String PUBLIC = "Public";
    public static String UNLISTED = "Unlisted";
    PicasawebService myService;
    /** Creates a new instance of Album */
    
    public Album(Credentials credentials) {
        this.credentials = credentials;
        myService = new PicasawebService("Picaja");
        try {
            myService.setUserCredentials(credentials.getUsername()+"@gmail.com", credentials.getPassword());
            System.out.println("Setting credentials");
        } catch (AuthenticationException ex) {
            ex.printStackTrace();
        }
    }
    
    public boolean addNew(String name, String access){
        boolean retval=false;
        try {
            String feed = "http://picasaweb.google.com/data/feed/api/user/"+credentials.getUsername();
            URL postUrl = new URL(feed);
            AlbumEntry myEntry = new AlbumEntry();
            myEntry.setAccess(access);
            myEntry.setTitle(new PlainTextConstruct(name));
            Person author = new Person(credentials.getUsername(), null, credentials.getUsername()+"@gmail.com");
            myEntry.getAuthors().add(author);
            PicasawebService myService = new PicasawebService("Picaja");
            myService.setUserCredentials(credentials.getUsername()+"@gmail.com", credentials.getPassword());
            AlbumEntry insertedEntry = myService.insert(postUrl, myEntry);
            retval = true;
        } catch (Exception e) {
            e.printStackTrace();
            retval = false;
        }
        return retval;
    }
    
    public List getAlbums(){
        List<AlbumEntry> albums = null;
        try {
            String feed = "http://picasaweb.google.com/data/feed/api/user/"+credentials.getUsername()+"?kind=album";
            URL albumsUrl = new URL(feed);
            UserFeed myUserFeed = myService.getFeed(albumsUrl, UserFeed.class);
            albums = myUserFeed.getAlbumEntries();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return albums;
    }
    
    public boolean exists(String album){
        boolean retval = false;
        int j=0;
        List<AlbumEntry> lst = getAlbums();
        for(int i=0;i<lst.size();i++){
            if(lst.get(i).getTitle().getPlainText().equals(album)){
                j++;
            }
        }
        if(j>0){
            retval = true;
        }
        return retval;
    }
    
    public boolean addPictures(File[] pictures, String album){
        boolean retval=false;
        try {
            String feed = "http://picasaweb.google.com/data/feed/api/user/"+credentials.getUsername();
            for(File picture: pictures){
                MediaFileSource myMedia = new MediaFileSource(picture, "image/jpeg");
                System.out.println("Inserting picture " + picture.getAbsolutePath());
                PhotoEntry returnedPhoto = myService.insert(new URL(feed+"/album/"+album), PhotoEntry.class, myMedia);
                System.out.println("Inserted picture " + picture.getAbsolutePath());
            }
            retval=true;
        } catch (Exception e) {
            e.printStackTrace();
            retval=false;
        }
        return retval;
    }
    
    public boolean addPicture(File picture, String album){
        boolean retval=false;
        try {
            String feed = "http://picasaweb.google.com/data/feed/api/user/"+credentials.getUsername();
            MediaFileSource myMedia = new MediaFileSource(picture, "image/jpeg");
            PhotoEntry returnedPhoto = myService.insert(new URL(feed+"/album/"+album), PhotoEntry.class, myMedia);
            retval=true;
        } catch (Exception e) {
            e.printStackTrace();
            retval=false;
        }
        return retval;
    }
    
}
